import 'package:common_utils/common_utils.dart';
import 'package:yp/config/config.dart';
// import 'package:yp/pages/main/logic.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../helper.dart';
import 'state.dart';

class SecKillLogic extends GetxController {
  final SecKillState state = SecKillState();
  // final MainAppLogic mainAppLogics = Get.put(MainAppLogic());
  // final MainLogic mainLogic = Get.put(MainLogic());

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    // 查询秒杀活动
    // querySeckillPage();
  }

  void querySeckillPage(callback) {}

  void setSlideshow(event, index) {
    // print(event['results']);
    List imgList = event[index]['slide_images'].split(',');
    LogUtil.d("==imgList===：${imgList.toString()}");
    state.imageMap = [];
    if (imgList.length > 0 && imgList != null) {
      for (var i = 0; i < imgList.length; i++) {
        state.imageMap.add({"id": i.toString(), 'logo': imgList[i]});
      }
    }
    print('state.imageMap');
    print(state.imageMap);
    update();
  }

  // 查询产品
  void queryProduct(index) {
    print(state.secKillEvent[index]);
    List results = Get.arguments!=null?Get.arguments['model']['results']:[];
    state.shopId =(results==null || results.length==0)? SpUtil.getInt('storeShopNum').toString():results[index]['ec_shop_num_id'].toString();
    print('========queryProduct=========>');
    print(state.shopId);
    var params = {
      "use_platform": 2,
      "ecactivity_num_id": state.secKillEvent[index]['ecactivity_id'], //活动编码
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id":(results==null || results.length==0)? SpUtil.getInt('storeShopNum'):results[index]['ec_shop_num_id'],
      "page_no": 1,
      "page_size": 1000,
      "customer_type": 3,
      // "user_num_id": SpUtil.getInt('empeNumId')
    };

    state.products = [];
    LogUtil.d("秒杀活动参数：$params");
    Api.secKillProduct(params).listen((event) {
      LogUtil.d("秒杀活动返回结果：$event");
      if (event['code'] == 0) {
        print('========queryProduct event=========>');
        if (event['seckill_activity'] == null) {
          return;
        }
        if (event['seckill_activity']['items'] != null) {
          state.products = event['seckill_activity']['items'];
        }
        state.actMap = event['seckill_activity'];
        // print(event);
        update();
      }
    });
  }

  // // 加入购物车
  // void requestAddGoods(int itemNumId, int styleNumId, int ecStyleNumId) {
  //   // String shopId = SpUtil.getInt('storeShopNum').toString();
  //   int shopId =SpUtil.getInt('storeShopNum');
  //   var params = {
  //     "use_platform": 2,
  //     "usr_num_id": SpUtil.getInt('empeNumId'),
  //     "emp_num_id": 0,
  //     "channel_num_id": 99,
  //     "quantity": 1.0,
  //     "ec_shop_num_id": shopId,
  //     "item_num_id": itemNumId,
  //     "style_num_id": styleNumId,
  //     "ec_style_num_id": ecStyleNumId,
  //     'access_token': SpUtil.getString('token'),
  //   };
  //   Api.addShoppingCart(params).listen((event) {
  //     if (event['code'] == 0) {
  //       mainLogic.getCartNum();
  //       toast('添加成功');
  //     } else {
  //       toast(event['message']);
  //     }
  //   });
  // }
}
